package com.mp3s.dc.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.DigestUtil;

public class UUID {
    /**
     * 获取机器的唯一标识（如 MAC 地址）
     * @return 机器的唯一标识字符串
     */
    public static String getUUID() {
        try {
            InetAddress ip = InetAddress.getLocalHost();
            NetworkInterface network = NetworkInterface.getByInetAddress(ip);
            if (network != null) {
                byte[] mac = network.getHardwareAddress();
                if (mac != null) {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 0; i < mac.length; i++) {
                        sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));
                    }
                    String uuid = sb.toString();
                    String uniqueCode = DigestUtil.md5Hex(uuid);
                    return uniqueCode;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String uuid =  StrUtil.uuid();
        String uniqueCode = DigestUtil.md5Hex(uuid);
        return uniqueCode;
    }
}
